<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/9
 * Time: 11:29
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class ProfessionModel
{
    const TABLE = 'profession';

    /**
     * @param array $data
     * @return array
     */
    public function info($data = [])
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function lists(array $param =[])
    {
        if (!empty($param['length']) && !empty($param['page'])){
            $limit = config('yucheng.limit');
            $start = 0;
            if (!empty($param['length'])) {
                $limit = $param['length'];
            }
            if (!empty($param['page'])) {
                $start = ($param['page']-1)*$limit;
            }

            return DB::table(self::TABLE)
                ->offset($start)->limit($limit)
                ->orderBy('order','desc')
                ->get()->toArray();
        }else{
            return DB::table(self::TABLE)
                ->orderBy('order','desc')
                ->get()->toArray();
        }
    }

    /**
     * @return mixed
     */
    public function countLists()
    {
        return DB::table(self::TABLE)->count();
    }

    /**
     * @param $data
     * @return mixed
     */
    public function selectLists($data = [])
    {
        return DB::table(self::TABLE)->where(function ($query) use ($data){
            if (count($data) > 0){
                $query->whereIn('id',$data);
            }
            $query->where('isManager',0);
        })->get()->toArray();
    }

    /**
     * @param array $check
     * @param $id
     * @return array
     */
    public function checkRepeat(array $check, $id = 0)
    {
        $result = DB::table(self::TABLE)->where($check)
            ->where(function ($query) use ($id){
                if ($id != 0){
                    $query->where('id','!=',$id);
                }
            })
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(Array $data)
    {
        if (!isset($data['order']) || empty($data['order'])){
            $order = 1;
        }else{
            $order = $data['order'];
        }
        $insertData = [
            'name' => $data['name'],
            'order' => $order,
            'isManager' => $data['isManager'],
            'isMajor' => $data['isMajor'],
        ];
        return DB::table(self::TABLE)->insertGetId($insertData);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where,array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }
}